package com.example.cloud.handler;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.exception.NoServiceAccessException;
import com.example.cloud.exception.TokenEmptyException;
import com.example.cloud.exception.TokenIllegalException;
import com.example.cloud.exception.UserInvalidException;
import com.example.cloud.exception.UserFailureException;
import com.example.cloud.model.dto.BaseResponse;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.FileNotFoundException;

/**
 * Created by wangtao on 2018/3/3.
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    /**
     * 自定义异常(未查询到用户)
     * @param ex
     * @return
     */
    @ExceptionHandler(UserInvalidException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public BaseResponse userInvalidExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_USER_INVALID_CODE,ex.getMessage());
    }

    /**
     * 自定义异常(用户session失效)
     * @param ex
     * @return
     */
    @ExceptionHandler(UserFailureException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public BaseResponse userFailureExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_SESSION_USER_NOT_FOUND_CODE,ex.getMessage());
    }
    /**
     * 自定义异常(token不存在)
     * @param ex
     * @return
     */
    @ExceptionHandler(TokenEmptyException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResponse tokenEmptyExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_TOKEN_EMPTY_CODE,ex.getMessage());
    }
    /**
     * 自定义异常(token验证失败)
     * @param ex
     * @return
     */
    @ExceptionHandler(TokenIllegalException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResponse tokenIllegalExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_TOKEN_ILLEGAL_CODE,ex.getMessage());
    }

    /**
     * 自定义异常(没有服务访问权限)
     * @param ex
     * @return
     */
    @ExceptionHandler(NoServiceAccessException.class)
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    public BaseResponse noServiceAuthExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_NO_SERVICE_ACCESS_CODE,ex.getMessage());
    }

    /**
     * 运行时异常(文件不存在异常)
     * @param ex
     * @return
     */
    @ExceptionHandler(FileNotFoundException.class)
    @ResponseStatus(value = HttpStatus.FORBIDDEN)
    public BaseResponse fileNotFoundExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_NOT_FOUND_CODE,ex.getMessage());
    }

    /**
     * 运行时异常(token过期异常)
     * @param ex
     * @return
     */
    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResponse expiredJwtExceptionHandler(Exception ex) {
        return new BaseResponse(CommonConstants.EX_TOKEN_EXPIRED_CODE,"token已过期!");
    }
}
